package com.xz.instance;

import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

/**
 * Created by YLZX-Z0107 on 2014/9/9.
 */
public class Client {
    private static CloseableHttpClient httpclient = HttpClients.createDefault();
    private static HttpGet get = new HttpGet();

    public static void setUri(String uri) {
        try {
            get.setURI(new URI(uri));
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }

    public static String getResponseContent(CloseableHttpClient client, HttpRequestBase method) {
        String result = null;
        try {
            CloseableHttpResponse response = client.execute(method);
            result = EntityUtils.toString(response.getEntity(), "utf-8");
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                System.out.println("get success!");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }
}
